import java.sql.*;
import java.io.Serializable;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.event.ActionEvent;

@ManagedBean(name = "agentBean")
@ViewScoped
public class Agent extends User implements Serializable {
	private String full_name;
	private String mobile_phone_number;
	private String agency_phone_number;
	private Integer city;
	private String adress;
	private String e_mail;
	private String confirm_password;
	private String msg;

	// name
	public void setFull_name(String full_name) {
		this.full_name = full_name;
	}

	public String getFull_name() {
		return full_name;
	}

	// mobile phone
	public void setMobile_phone_number(String mobile_phone_number) {
		this.mobile_phone_number = mobile_phone_number;
	}

	public String getMobile_phone_number() {
		return mobile_phone_number;
	}

	// agency phone
	public void setAgency_phone_number(String agency_phone_number) {
		this.agency_phone_number = agency_phone_number;
	}

	public String getAgency_phone_number() {
		return agency_phone_number;
	}

	// city
	public void setCity(Integer city) {
		this.city = city;
	}

	public Integer getCity() {
		return city;
	}

	// adress
	public void setAdress(String adress) {
		this.adress = adress;
	}

	public String getAdress() {
		return adress;
	}

	// e-mail
	public void setE_mail(String e_mail) {
		this.e_mail = e_mail;
	}

	public String getE_mail() {
		return e_mail;
	}

	// confirm password
	public void setConfirm_password(String confirm_password) {
		this.confirm_password = confirm_password;
	}

	public String getConfirm_password() {
		return confirm_password;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public String getMsg() {
		return msg;
	}

	public void submitRegisterButton(ActionEvent event) {

		if (this.getPassword().equals(confirm_password)) {
			
			try {
				Class.forName("com.mysql.jdbc.Driver").newInstance();
				Connection con = DriverManager.getConnection(
						"jdbc:mysql://localhost:3306/estate_agency_system",
						"root", "admin");
				
				String query = "INSERT INTO registration(Status, idAgent) VALUES(?, ?)";
				PreparedStatement statement = con.prepareStatement(query);
				statement.setString(1, "Non-Registered");
				statement.setString(2, this.getId());
				statement.executeUpdate();
				
				String q = "INSERT INTO agent(idAgent, AgentName, AgentPhone, AgencyPhone, CityID, AgencyAddress, Mail, Password) VALUES(?,?,?,?,?,?,?,?)";
				PreparedStatement s = con.prepareStatement(q);
				s.setString(1, this.getId());
				s.setString(2, this.getFull_name());
				s.setString(3, this.getMobile_phone_number());
				s.setString(4, this.getAgency_phone_number());
				s.setInt(5, this.getCity());
				s.setString(6, this.getAdress());
				s.setString(7, this.getE_mail());
				s.setString(8, this.getPassword());
				s.executeUpdate();
				
			} catch (InstantiationException e) {
				// TODO Auto-generated catch block
				System.out.println("111" + e);
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				System.out.println("222" + e);
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				System.out.println("333" + e);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				System.out.println("444" + e);
			}
			
			
			
			
		}

		this.setMsg("Your registration completed...");
	}

}
